{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8061531d-7307-4d34-a10d-e3a409a22ea1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>customer_months</th>\n",
       "      <th>policy_state</th>\n",
       "      <th>policy_csl</th>\n",
       "      <th>policy_deductable</th>\n",
       "      <th>policy_annual_premium</th>\n",
       "      <th>umbrella_limit</th>\n",
       "      <th>insured_zip</th>\n",
       "      <th>insured_sex</th>\n",
       "      <th>insured_education_level</th>\n",
       "      <th>...</th>\n",
       "      <th>auto_make</th>\n",
       "      <th>auto_model</th>\n",
       "      <th>auto_year</th>\n",
       "      <th>fraud</th>\n",
       "      <th>policy_bind_date_year</th>\n",
       "      <th>policy_bind_date_month</th>\n",
       "      <th>policy_bind_date_day</th>\n",
       "      <th>incident_date_year</th>\n",
       "      <th>incident_date_month</th>\n",
       "      <th>incident_date_day</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>37</td>\n",
       "      <td>189</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1000</td>\n",
       "      <td>1465.71</td>\n",
       "      <td>5000000</td>\n",
       "      <td>455456</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>9</td>\n",
       "      <td>26</td>\n",
       "      <td>2000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>44</td>\n",
       "      <td>234</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>500</td>\n",
       "      <td>821.24</td>\n",
       "      <td>0</td>\n",
       "      <td>591805</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>1996</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>33</td>\n",
       "      <td>23</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1000</td>\n",
       "      <td>1844.00</td>\n",
       "      <td>0</td>\n",
       "      <td>442490</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>7</td>\n",
       "      <td>36</td>\n",
       "      <td>2002</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>42</td>\n",
       "      <td>210</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>500</td>\n",
       "      <td>1867.29</td>\n",
       "      <td>0</td>\n",
       "      <td>439408</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>11</td>\n",
       "      <td>21</td>\n",
       "      <td>2003</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>29</td>\n",
       "      <td>81</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1000</td>\n",
       "      <td>816.25</td>\n",
       "      <td>0</td>\n",
       "      <td>640575</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>5</td>\n",
       "      <td>14</td>\n",
       "      <td>2004</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295</th>\n",
       "      <td>36</td>\n",
       "      <td>30</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2000</td>\n",
       "      <td>1384.15</td>\n",
       "      <td>9000000</td>\n",
       "      <td>593323</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>27</td>\n",
       "      <td>2002</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>296</th>\n",
       "      <td>47</td>\n",
       "      <td>285</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>500</td>\n",
       "      <td>1590.78</td>\n",
       "      <td>7000000</td>\n",
       "      <td>447235</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>7</td>\n",
       "      <td>17</td>\n",
       "      <td>1999</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>297</th>\n",
       "      <td>39</td>\n",
       "      <td>256</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2000</td>\n",
       "      <td>1265.24</td>\n",
       "      <td>0</td>\n",
       "      <td>592069</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>30</td>\n",
       "      <td>1997</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>298</th>\n",
       "      <td>35</td>\n",
       "      <td>54</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>500</td>\n",
       "      <td>1229.74</td>\n",
       "      <td>0</td>\n",
       "      <td>451451</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>9</td>\n",
       "      <td>26</td>\n",
       "      <td>2012</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>299</th>\n",
       "      <td>34</td>\n",
       "      <td>154</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>500</td>\n",
       "      <td>1744.33</td>\n",
       "      <td>0</td>\n",
       "      <td>462941</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>1998</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 41 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     age  customer_months  policy_state  policy_csl  policy_deductable  \\\n",
       "0     37              189             2           2               1000   \n",
       "1     44              234             1           1                500   \n",
       "2     33               23             1           2               1000   \n",
       "3     42              210             0           2                500   \n",
       "4     29               81             0           0               1000   \n",
       "..   ...              ...           ...         ...                ...   \n",
       "295   36               30             1           2               2000   \n",
       "296   47              285             2           1                500   \n",
       "297   39              256             2           2               2000   \n",
       "298   35               54             2           0                500   \n",
       "299   34              154             2           0                500   \n",
       "\n",
       "     policy_annual_premium  umbrella_limit  insured_zip  insured_sex  \\\n",
       "0                  1465.71         5000000       455456            0   \n",
       "1                   821.24               0       591805            1   \n",
       "2                  1844.00               0       442490            0   \n",
       "3                  1867.29               0       439408            1   \n",
       "4                   816.25               0       640575            0   \n",
       "..                     ...             ...          ...          ...   \n",
       "295                1384.15         9000000       593323            0   \n",
       "296                1590.78         7000000       447235            1   \n",
       "297                1265.24               0       592069            0   \n",
       "298                1229.74               0       451451            0   \n",
       "299                1744.33               0       462941            1   \n",
       "\n",
       "     insured_education_level  ...  auto_make  auto_model  auto_year  fraud  \\\n",
       "0                          5  ...          9          26       2000    0.0   \n",
       "1                          3  ...          6          10       1996    0.0   \n",
       "2                          2  ...          7          36       2002    0.0   \n",
       "3                          3  ...         11          21       2003    1.0   \n",
       "4                          4  ...          5          14       2004    0.0   \n",
       "..                       ...  ...        ...         ...        ...    ...   \n",
       "295                        1  ...          4          27       2002    NaN   \n",
       "296                        4  ...          7          17       1999    NaN   \n",
       "297                        0  ...          4          30       1997    NaN   \n",
       "298                        4  ...          9          26       2012    NaN   \n",
       "299                        5  ...          6          10       1998    NaN   \n",
       "\n",
       "     policy_bind_date_year  policy_bind_date_month  policy_bind_date_day  \\\n",
       "0                     1970                       1                     1   \n",
       "1                     1970                       1                     1   \n",
       "2                     1970                       1                     1   \n",
       "3                     1970                       1                     1   \n",
       "4                     1970                       1                     1   \n",
       "..                     ...                     ...                   ...   \n",
       "295                   1970                       1                     1   \n",
       "296                   1970                       1                     1   \n",
       "297                   1970                       1                     1   \n",
       "298                   1970                       1                     1   \n",
       "299                   1970                       1                     1   \n",
       "\n",
       "     incident_date_year  incident_date_month  incident_date_day  \n",
       "0                  1970                    1                  1  \n",
       "1                  1970                    1                  1  \n",
       "2                  1970                    1                  1  \n",
       "3                  1970                    1                  1  \n",
       "4                  1970                    1                  1  \n",
       "..                  ...                  ...                ...  \n",
       "295                1970                    1                  1  \n",
       "296                1970                    1                  1  \n",
       "297                1970                    1                  1  \n",
       "298                1970                    1                  1  \n",
       "299                1970                    1                  1  \n",
       "\n",
       "[1000 rows x 41 columns]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd #导入数据包\n",
    "#数据预处理\n",
    "train=pd.read_csv('train.csv')\n",
    "test=pd.read_csv('test.csv')\n",
    "submission=pd.read_csv('submission.csv')\n",
    "#合并训练集和测试集同时进行数据预处理\n",
    "data=pd.concat([train,test],axis=0)\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "#标签编码\n",
    "numberical_fea=list(data.select_dtypes(include=['object']).columns)\n",
    "division_le=LabelEncoder()\n",
    "for fea in numberical_fea:\n",
    "    division_le.fit(data[fea].values)\n",
    "    data[fea]=division_le.transform(data[fea].values)\n",
    "    #拆分数据集\n",
    "testA=data[data['fraud'].isnull()].drop(['fraud'],axis=1)\n",
    "trainA=data[data['fraud'].notnull()]\n",
    "\n",
    "import datetime # 将日期列分解成年、月、日  \n",
    "data['policy_bind_date'] = pd.to_datetime(data['policy_bind_date'])  \n",
    "data['policy_bind_date_year'] = data['policy_bind_date'].dt.year.astype('int64')  \n",
    "data['policy_bind_date_month'] = data['policy_bind_date'].dt.month.astype('int64')  \n",
    "data['policy_bind_date_day'] = data['policy_bind_date'].dt.day.astype('int64')  \n",
    "data['incident_date'] = pd.to_datetime(data['incident_date'])  \n",
    "data['incident_date_year'] = data['incident_date'].dt.year.astype('int64')  \n",
    "data['incident_date_month'] = data['incident_date'].dt.month.astype('int64')  \n",
    "data['incident_date_day'] = data['incident_date'].dt.day.astype('int64')  \n",
    "data['auto_year'] = data['auto_year'].astype('int64')  \n",
    "data=data.drop(['policy_id','policy_bind_date','incident_date'],axis=1)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a17e8733-c361-4bbd-a839-197328d78ef8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>customer_months</th>\n",
       "      <th>policy_state</th>\n",
       "      <th>policy_csl</th>\n",
       "      <th>policy_deductable</th>\n",
       "      <th>policy_annual_premium</th>\n",
       "      <th>umbrella_limit</th>\n",
       "      <th>insured_zip</th>\n",
       "      <th>insured_sex</th>\n",
       "      <th>insured_education_level</th>\n",
       "      <th>...</th>\n",
       "      <th>fraud</th>\n",
       "      <th>policy_bind_date_year</th>\n",
       "      <th>policy_bind_date_month</th>\n",
       "      <th>policy_bind_date_day</th>\n",
       "      <th>incident_date_year</th>\n",
       "      <th>incident_date_month</th>\n",
       "      <th>incident_date_day</th>\n",
       "      <th>injury_claim_per</th>\n",
       "      <th>property_claim_per</th>\n",
       "      <th>vehicle_claim_per</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>37</td>\n",
       "      <td>189</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1000</td>\n",
       "      <td>1465.71</td>\n",
       "      <td>5000000</td>\n",
       "      <td>455456</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.109758</td>\n",
       "      <td>0.104715</td>\n",
       "      <td>0.809248</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>44</td>\n",
       "      <td>234</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>500</td>\n",
       "      <td>821.24</td>\n",
       "      <td>0</td>\n",
       "      <td>591805</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.106077</td>\n",
       "      <td>0.200395</td>\n",
       "      <td>0.736918</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>33</td>\n",
       "      <td>23</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1000</td>\n",
       "      <td>1844.00</td>\n",
       "      <td>0</td>\n",
       "      <td>442490</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.093249</td>\n",
       "      <td>0.193335</td>\n",
       "      <td>0.703423</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>42</td>\n",
       "      <td>210</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>500</td>\n",
       "      <td>1867.29</td>\n",
       "      <td>0</td>\n",
       "      <td>439408</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.160364</td>\n",
       "      <td>0.086477</td>\n",
       "      <td>0.777613</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>29</td>\n",
       "      <td>81</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1000</td>\n",
       "      <td>816.25</td>\n",
       "      <td>0</td>\n",
       "      <td>640575</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.094046</td>\n",
       "      <td>0.172471</td>\n",
       "      <td>0.695677</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295</th>\n",
       "      <td>36</td>\n",
       "      <td>30</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2000</td>\n",
       "      <td>1384.15</td>\n",
       "      <td>9000000</td>\n",
       "      <td>593323</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.215221</td>\n",
       "      <td>0.105835</td>\n",
       "      <td>0.740848</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>296</th>\n",
       "      <td>47</td>\n",
       "      <td>285</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>500</td>\n",
       "      <td>1590.78</td>\n",
       "      <td>7000000</td>\n",
       "      <td>447235</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.121959</td>\n",
       "      <td>0.122569</td>\n",
       "      <td>0.753621</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>297</th>\n",
       "      <td>39</td>\n",
       "      <td>256</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2000</td>\n",
       "      <td>1265.24</td>\n",
       "      <td>0</td>\n",
       "      <td>592069</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.136500</td>\n",
       "      <td>0.129832</td>\n",
       "      <td>0.822949</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>298</th>\n",
       "      <td>35</td>\n",
       "      <td>54</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>500</td>\n",
       "      <td>1229.74</td>\n",
       "      <td>0</td>\n",
       "      <td>451451</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.194537</td>\n",
       "      <td>0.095115</td>\n",
       "      <td>0.772917</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>299</th>\n",
       "      <td>34</td>\n",
       "      <td>154</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>500</td>\n",
       "      <td>1744.33</td>\n",
       "      <td>0</td>\n",
       "      <td>462941</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.108301</td>\n",
       "      <td>0.109533</td>\n",
       "      <td>0.775426</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 44 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     age  customer_months  policy_state  policy_csl  policy_deductable  \\\n",
       "0     37              189             2           2               1000   \n",
       "1     44              234             1           1                500   \n",
       "2     33               23             1           2               1000   \n",
       "3     42              210             0           2                500   \n",
       "4     29               81             0           0               1000   \n",
       "..   ...              ...           ...         ...                ...   \n",
       "295   36               30             1           2               2000   \n",
       "296   47              285             2           1                500   \n",
       "297   39              256             2           2               2000   \n",
       "298   35               54             2           0                500   \n",
       "299   34              154             2           0                500   \n",
       "\n",
       "     policy_annual_premium  umbrella_limit  insured_zip  insured_sex  \\\n",
       "0                  1465.71         5000000       455456            0   \n",
       "1                   821.24               0       591805            1   \n",
       "2                  1844.00               0       442490            0   \n",
       "3                  1867.29               0       439408            1   \n",
       "4                   816.25               0       640575            0   \n",
       "..                     ...             ...          ...          ...   \n",
       "295                1384.15         9000000       593323            0   \n",
       "296                1590.78         7000000       447235            1   \n",
       "297                1265.24               0       592069            0   \n",
       "298                1229.74               0       451451            0   \n",
       "299                1744.33               0       462941            1   \n",
       "\n",
       "     insured_education_level  ...  fraud  policy_bind_date_year  \\\n",
       "0                          5  ...    0.0                   1970   \n",
       "1                          3  ...    0.0                   1970   \n",
       "2                          2  ...    0.0                   1970   \n",
       "3                          3  ...    1.0                   1970   \n",
       "4                          4  ...    0.0                   1970   \n",
       "..                       ...  ...    ...                    ...   \n",
       "295                        1  ...    NaN                   1970   \n",
       "296                        4  ...    NaN                   1970   \n",
       "297                        0  ...    NaN                   1970   \n",
       "298                        4  ...    NaN                   1970   \n",
       "299                        5  ...    NaN                   1970   \n",
       "\n",
       "     policy_bind_date_month  policy_bind_date_day  incident_date_year  \\\n",
       "0                         1                     1                1970   \n",
       "1                         1                     1                1970   \n",
       "2                         1                     1                1970   \n",
       "3                         1                     1                1970   \n",
       "4                         1                     1                1970   \n",
       "..                      ...                   ...                 ...   \n",
       "295                       1                     1                1970   \n",
       "296                       1                     1                1970   \n",
       "297                       1                     1                1970   \n",
       "298                       1                     1                1970   \n",
       "299                       1                     1                1970   \n",
       "\n",
       "     incident_date_month  incident_date_day  injury_claim_per  \\\n",
       "0                      1                  1          0.109758   \n",
       "1                      1                  1          0.106077   \n",
       "2                      1                  1          0.093249   \n",
       "3                      1                  1          0.160364   \n",
       "4                      1                  1          0.094046   \n",
       "..                   ...                ...               ...   \n",
       "295                    1                  1          0.215221   \n",
       "296                    1                  1          0.121959   \n",
       "297                    1                  1          0.136500   \n",
       "298                    1                  1          0.194537   \n",
       "299                    1                  1          0.108301   \n",
       "\n",
       "     property_claim_per  vehicle_claim_per  \n",
       "0              0.104715           0.809248  \n",
       "1              0.200395           0.736918  \n",
       "2              0.193335           0.703423  \n",
       "3              0.086477           0.777613  \n",
       "4              0.172471           0.695677  \n",
       "..                  ...                ...  \n",
       "295            0.105835           0.740848  \n",
       "296            0.122569           0.753621  \n",
       "297            0.129832           0.822949  \n",
       "298            0.095115           0.772917  \n",
       "299            0.109533           0.775426  \n",
       "\n",
       "[1000 rows x 44 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#新增索赔金额占比\n",
    "data['injury_claim_per']=data['injury_claim']/data['total_claim_amount']\n",
    "data['property_claim_per']=data['property_claim']/data['total_claim_amount']\n",
    "data['vehicle_claim_per']=data['vehicle_claim']/data['total_claim_amount']\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d8cd972d-78dc-4024-a1f3-cb9504ef3fe1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "经过皮尔逊相关系数选择后的数据集的形状: (700, 15)\n",
      "选择的特征索引: Index(['policy_deductable', 'insured_education_level', 'insured_hobbies',\n",
      "       'incident_type', 'collision_type', 'incident_severity',\n",
      "       'authorities_contacted', 'incident_state', 'incident_city',\n",
      "       'number_of_vehicles_involved', 'witnesses', 'total_claim_amount',\n",
      "       'injury_claim', 'property_claim', 'vehicle_claim'],\n",
      "      dtype='object')\n"
     ]
    }
   ],
   "source": [
    "# 特征选择\n",
    "from sklearn.feature_selection import SelectKBest\n",
    "from sklearn.feature_selection import f_regression\n",
    "\n",
    "# 创建皮尔逊相关系数选择器对象，设定选择的特征数量\n",
    "selector = SelectKBest(f_regression, k=15)\n",
    "\n",
    "# 拟合数据并进行特征选择\n",
    "X = trainA.drop(['fraud'], axis=1)\n",
    "Y = trainA['fraud']\n",
    "X_selected = selector.fit_transform(X, Y)\n",
    "\n",
    "# 查看选择后的特征\n",
    "print(\"经过皮尔逊相关系数选择后的数据集的形状:\", X_selected.shape)\n",
    "mask = selector.get_support() # 获得特征掩码\n",
    "new_features = X.columns[mask] # 选择重要的特征\n",
    "print(\"选择的特征索引:\", new_features)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b4bde19f-4625-4b3e-b7e2-184c6f874b9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "#拆分训练集和测试集   \n",
    "test_2=data[data['fraud'].isnull()].drop(['fraud'],axis=1)  \n",
    "train_2=data[data['fraud'].notnull()]\n",
    "x_train_2=train_2.drop(['fraud'],axis=1)\n",
    "y_train_2=train_2['fraud']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7b0895d7-56b4-49e7-ba1b-9281eb5fe453",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[LightGBM] [Info] Number of positive: 181, number of negative: 519\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.001061 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 2804\n",
      "[LightGBM] [Info] Number of data points in the train set: 700, number of used features: 37\n",
      "[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.258571 -> initscore=-1.053407\n",
      "[LightGBM] [Info] Start training from score -1.053407\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n",
      "[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n"
     ]
    }
   ],
   "source": [
    "#算法一\n",
    "import lightgbm as lgb\n",
    "model_lgb= lgb.LGBMClassifier()\n",
    "#楼型谢练,适用默认参数\n",
    "model_lgb.fit(x_train_2, y_train_2)\n",
    "#AUC平测:proba进行捉交,结果会更好\n",
    "y_pred = model_lgb.predict_proba(test_2)\n",
    "result = pd.read_csv('submission.csv')\n",
    "result['fraud']= y_pred[ :,1]\n",
    "result.to_csv('lgb.csv',index=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4285d765-590e-44a4-85f9-154c96391875",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "knn = KNeighborsClassifier(n_neighbors=5)\n",
    "knn.fit(x_train_2, y_train_2)\n",
    "y_pred = knn. predict_proba(test_2)\n",
    "result = pd.read_csv( 'submission.csv')\n",
    "result['fraud']= y_pred[:,1]\n",
    "result.to_csv('knn.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "db805261-01e3-4c2c-b771-e3636b56b5d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "#采用默认参数进行分类\n",
    "rfc = RandomForestClassifier()\n",
    "rfc.fit(x_train_2,y_train_2)\n",
    "y_pred = rfc.predict_proba(test_2)\n",
    "result = pd.read_csv('submission.csv')\n",
    "result['fraud'] =y_pred[:,1]\n",
    "result.to_csv('rf.csv', index=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4590a323-79e7-490e-989d-92a014cb88b0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
